package com.xcity.game.item.effect;

import com.xcity.db.entity.Player;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.item.GameItem;
import com.xcity.game.item.ItemEffect;
import com.xcity.game.item.ItemException;
import com.xcity.game.item.listener.BagListener;

/**
 * 获得物品
 * @author YangLi
 *
 */
public class AddItemEffect implements ItemEffect {

	@Override
	public boolean effect(Player player, GameItem item, int count, BagListener listener) throws ItemException {
		if (!player.isOnline()) {
			throw new ItemException(MessageConst.PLAYER_OFFLINE);
		}
		int[] params = item.getTemplate().getParams();
		if (params != null) {
			for (int i = 0, len = params.length; i < len; i += 2) {
				int id = params[i];
				int c = params[i + 1] * count;
				player.getBag().addItem(id, c, Cause.USE_ITEM, listener);
			}
			return true;
		}
		return false;
	}

}
